<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>流程实例管理</title>
    <#include "/pubHeadStyle.html">
</head>
<body>
<div style="height: 100%;">
    <div class="tablesize roles" style=" padding:10px">
        <table id="processInstanceTable" lay-filter="processInstanceTable"></table>
    </div>
</div>
<script type="text/html" id="headerToolsBar">
    <div class="ccpui-enterSel">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-sm" lay-event="query">
            <i class="layui-icon layui-icon-search"></i>
            <span>查询</span>
        </a>
        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">
            <i class="layui-icon layui-icon-delete"></i>
            <span>删除</span>
        </a>
        <a class="layui-btn layui-btn-sm" lay-event="reset">
            <i class="layui-icon layui-icon-refresh"></i>
            <span>重置</span>
        </a>
    </div>

    <form class="layui-form" action="" lay-filter="instanceQueryForm" style="margin-top:10px">
        <div class="table-top-filter">
            <div class="table-filter-alone">
                <label class="layui-form-label">流程实例标题</label>
                <input type="text" name="Q_subject_SL" id="subject" class="layui-input filter-value"
                       placeholder="流程实例标题"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">流程定义名称</label>
                <input type="text" name="Q_processName_SL" id="processName" class="layui-input filter-value"
                       placeholder="流程定义名称"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">流程运行ID</label>
                <input type="text" name="Q_runId_SL" id="runId" class="layui-input filter-value" placeholder="流程运行ID"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">流程实例ID</label>
                <input type="text" name="Q_procInstId_SL" id="procInstId" class="layui-input filter-value"
                       placeholder="流程实例ID"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">流程业务ID</label>
                <input type="text" name="Q_businessKey_SL" id="businessKey" class="layui-input filter-value"
                       placeholder="流程业务ID"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">创建时间</label>
                <input type="text" name="Q_createTime_DL" id="createTime" class="layui-input filter-value"
                       placeholder="开始时间"/>
                <span style="float:left;margin-left:46px">至</span>
                <input type="text" name="Q_endCreateTime_DG" id="endCreateTime" class="layui-input filter-value"
                       placeholder="结束时间"
                       style="margin-left:60px"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">状态</label>
                <select name="Q_status_SN" id="status" class="layui-input filter-value">
                    <option value="">所有</option>
                    <option value="1">正在运行</option>
                    <option value="2">结束</option>
                    <option value="3">人工结束</option>
                    <option value="5">撤销</option>
                    <option value="6">驳回</option>
                    <option value="7">追回</option>
                </select>

            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">工单号</label>
                <input type="text" name="Q_globalFlowNo_SL" id="globalFlowNo" class="layui-input filter-value"
                       placeholder="工单号"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">创建人</label>
                <input type="text" name="Q_creator_SL" id="creator" class="layui-input filter-value" placeholder="创建人"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">创建部门</label>
                <input type="text" name="Q_startOrgName_SL" id="startOrgName" class="layui-input filter-value"
                       placeholder="创建部门"/>
            </div>
        </div>
    </form>
    </div>
</script>

<!--表格序列号-->
<script type="text/html" id="serialNum">
    {{d.LAY_TABLE_INDEX + 1}}
</script>

<script type="text/html" id="managerBar">
    {{#  if(d.authorizeRight && d.authorizeRight.instanceDel == 'Y'){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {{#  } }}
    {{#  if(d.authorizeRight && d.authorizeRight.instanceLog == 'Y'){ }}
    <a class="layui-btn layui-btn-xs" lay-event="log">操作日志</a>
    {{#  } }}

</script>


<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','table', 'layer', 'util', 'commons', 'setter', 'totalFilter', 'common', 'laydate','admin'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var $ = layui.$;
        var laydate = layui.laydate;
        var setter = layui.setter;
        var commons = layui.commons;
        var common = layui.common;
        var totalFilter = layui.totalFilter;
        var admin = layui.admin;

        var tableH = commons.tabHeight();

        var dataPrivacyLevel;
        var privacyLevel = ${privacyLevel};

        loadTable(serverPath.workflowPath + setter.workflowLinkPath + 'processRun/processInstance');


        //第一个实例
        function loadTable(url) {
        var tableResize = table.render({
            elem: '#processInstanceTable',
            id: 'processInstanceTable'
            , cellMinWidth: 80
            , title: '流程实例列表'
            , height: tableH
            , url: url
            , method: 'post'
            , toolbar: '#headerToolsBar'
            ,autoSort: false
            ,defaultToolbar: ['filter', 'print']
            , page: true
            , cols: [[
                {type: 'checkbox'}
                , {field: 'runId', title: '序号', align: 'center', templet: '#serialNum'}
                , {field: 'globalFlowNo', title: '工单号', sort: true,minWidth: 100, align: 'center'}
                , {field: 'subject', title: '流程实例标题', sort: true, align: 'center', event: 'get',minWidth: 230, style: 'color:#1E9FFF;cursor:pointer'}
                , {
                    field: 'processName',
                    title: '流程定义名称',
                    sort: true,
                    align: 'center',
                    event: 'detail',
                    style: 'color:#1E9FFF;cursor:pointer'
                }
                , {field: 'creator', title: '创建人', sort: true, align: 'center'}
                , {
                    field: 'createTime', title: '创建时间',minWidth: 200, sort: true, align: 'center', templet: function (d) {
                        if (d.createTime== undefined) {
                            return "";
                        } else {
                            return util.toDateString(d.createTime.time, "yyyy-MM-dd HH:mm:ss");
                        }
                    }
                }
                , {
                    field: 'endTime', title: '结束时间',minWidth: 200, sort: true, align: 'center', templet: function (d) {
                        if (d.endTime == undefined) {
                            return "";
                        } else {
                            return util.toDateString(d.endTime.time, "yyyy-MM-dd HH:mm:ss");
                        }
                    }
                }
                , {
                    field: 'duration', title: '持续时间', sort: true, align: 'center', templet: function (d) {
                        if (d.duration === undefined||d.duration===0) {
                            return "";
                        }else {
                           // return Math.floor(d.duration / 1440) + "天" + Math.floor(d.duration % 1440 / 60) + "时" + Math.floor(d.duration % 1440 % 60) + "分";
                            //计算出相差天数
                            var days = Math.floor(d.duration / (24 * 60 * 60 * 1000));
                            //计算出小时数
                            var leave1 = d.duration % (24 * 60 * 60 * 1000);   //计算天数后剩余的毫秒数
                            var hours = Math.floor(leave1 / (60 * 60 * 1000));
                            //计算相差分钟数
                            var leave2 = leave1 % (60 * 60 * 1000);        //计算小时数后剩余的毫秒数
                            var minutes = Math.floor(leave2 / (60 * 1000));

                            return days + "天" + hours + "时" + minutes + "分";
                        }
                    }
                }
                , {
                    field: 'status', title: '状态', sort: true, align: 'center', templet: function (d) {
                        if (d.status == undefined) {
                            return "";
                        }
                        if (d.status == 1) {
                            return '<span style="color: #15cc08;">正在运行</span>';
                        }
                        if (d.status == 2) {
                            return '<span style="color: #cc0c00;">结束</span>';
                        }
                        if (d.status == 3) {
                            return '<span style="color: rgba(123,73,0,0.94);">人工结束</span>';
                        }
                        if (d.status == 5) {
                            return '<span style="color: #15cc08;">撤销</span>';
                        }
                        if (d.status == 6) {
                            return '<span style="color: #15cc08;">驳回</span>';
                        }
                        if (d.status == 7) {
                            return '<span style="color: #15cc08;">追回</span>';
                        }
                    }
                }

                , { title: '管理', toolbar: '#managerBar', width: 150, align: 'center'}
            ]]
            ,done:function () {
                table.resize('processInstanceTable');
                commons.reflashDate({startName: '#createTime', endName: '#endCreateTime'});
                common.tableSelectCon('.ccpui-enterSel .layui-form input','a[lay-event=query]','.ccpui-enterSel');

            }
        });
            totalFilter.init('processInstanceTable');
    }
        table.on('toolbar(processInstanceTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data; //获取选中行数据
            switch (obj.event) {
                case 'query':
                    common.tableConditionRender("processInstanceTable", "instanceQueryForm", {where: {}}, false);
                    break;
                case 'del':
                    if (data.length === 0) {
                        layer.msg('请先选择一行数据！');
                    } else {
                        var seleIds = [];
                        for (var i = 0; i < data.length; i++) {
                            seleIds.push(data[i].runId);
                        }
                        var s = seleIds.join(",");
                        layer.confirm('确定删除当前数据吗？', {title: '删除确认'}, function (index) {
                            $.ajax({
                                url: serverPath.workflowPath + setter.workflowLinkPath + 'processRun/del?runId=' + s,
                                type: "DELETE",
                                dataType: 'json',
                                success: function (result) {
                                    if(result.code == 0){
                                        //成功后的提示
                                        layer.alert(result.msg);
                                        //执行重载
                                        table.reload('processInstanceTable', {
                                            page: {curr: 1, limit: 10}
                                        });
                                        totalFilter.init('processInstanceTable');
                                    }else {
                                        layer.alert("删除失败");
                                    }

                                },
                                error: function (XMLHttpRequest) {
                                    layer.alert("请求失败！" + XMLHttpRequest.status);
                                }
                            })
                        })
                    }
                    break;
                case 'reset':
                    common.tableConditionRender("processInstanceTable", "instanceQueryForm", {where: {}}, true);
                    break;
            }
        });
        //监听行工具事件
        table.on('tool(processInstanceTable)', function (obj) {
            var data = obj.data;
            var runId = data.runId;
            var procInstId = data.procInstId;
            // 获取密级
            $.ajax({
                url: serverPath.workflowPath + setter.workflowLinkPath+"processRun/getPrivacyLevelByRunId",
                data:{runId: runId},
                type: "post",
                dataType: 'json',
                success: function (result) {
                    dataPrivacyLevel = result;
                    if (privacyLevel < dataPrivacyLevel) {
                        layer.msg('密级不足,无法执行该操作', {icon: 5});
                    }else{
                        switch (obj.event) {
                            case 'del':
                                layer.confirm("确认要删除该实例吗？", {title: '删除确认'}, function (index) {
                                        $.ajax({
                                            url: serverPath.workflowPath + setter.workflowLinkPath + "processRun/del?runId=" + runId,
                                            type: "DELETE",
                                            dataType: 'json',
                                            success: function (data) {
                                                //成功后的提示
                                                layer.alert(data.msg);
                                                //执行重载
                                                table.reload('processInstanceTable', {
                                                    page: {curr: 1, limit: 10}
                                                });
                                                totalFilter.init('processInstanceTable');
                                            },
                                            error: function () {
                                                layer.alert("删除失败");
                                            }
                                        });
                                    }
                                );
                                break;
                            case 'detail':
                                var isModify = true;
                                layer.open({
                                    type: 2,
                                    title: "流程明细",
                                    shade: 0.3,
                                    left: 200,
                                    area: ['100%', '100%'],
                                    content: serverPath.workflowPath +  setter.workflowLinkPath + 'processRun/view/info?runId=' + runId,
                                    cancel: function(index, layero){
                                        isModify = false;
                                    },
                                    end:function () {
                                        if (isModify) {
                                            table.reload('processInstanceTable', {
                                                page: {
                                                    curr: 1
                                                }
                                            });
                                            totalFilter.init('processInstanceTable');
                                        }
                                    }
                                });

                                break;
                            case 'get':
                                window.open(serverPath.workflowPath +setter.workflowLinkPath +'processRun/view/get?runId=' + runId + '&procInstId=' + procInstId);
                                break;
                            case 'log':
                                layer.open({
                                    type: 2,
                                    title: "操作日志",
                                    shade: 0.3,
                                    left: 200,
                                    area: ['80%', '80%'],
                                    content:serverPath.workflowPath +  setter.workflowLinkPath + 'processRun/view/form?runId=' + data.runId
                                });
                                break;
                        }
                    }
                }
            });

        });

        commons.reflashDate({startName: '#createTime', endName: '#endCreateTime'});
        window.Initialization = function () {
            commons.reflashDate({startName: '#createTime', endName: '#endCreateTime'});
        };
        common.tableSort("processInstanceTable",{show:window.Initialization});
    });


</script>
</body>
</html>
